const http = require("../../utils/http");

Page({
    data: {
        musics: []
    },
    onLoad() {
        var url = "/top/playlist?cat=华语&limit=15&offset=0";
        http({
            url: url,
            callback: res => {
                this.handleHttpData(res);
            }
        })
    },
    handleHttpData(res) {
        let musics = [];
        let playlists = res.data.playlists;
        playlists.forEach((value, index) => {
            let id = value.id;
            let name = value.name;
            let coverImage = value.coverImgUrl;
            musics.push({
                id: id,
                name: name,
                coverImage: coverImage
            })
        })
        this.setData({
            musics: musics
        })
    },
    // 上拉刷新的逻辑
    onReachBottom() {
        wx.showLoading({
            title:"数据加载中"
        });
        var offset = this.data.musics.length;
        var url = "/top/playlist?cat=华语&limit=15&offset=" + offset;
        console.log(url)
        http({
            url: url,
            // 请求的数据添加到数组中
            callback: res => {
                let loads = [];
                let playlists = res.data.playlists;
                playlists.forEach((value, index) => {
                    let id = value.id;
                    let name = value.name;
                    let coverImage = value.coverImgUrl;
                    loads.push({
                        id: id,
                        name: name,
                        coverImage: coverImage
                    })
                })
                let musics = this.data.musics;
                let total  = musics.concat(loads);
                this.setData({
                    musics:total
                })
                wx.hideLoading();
            }
        })
    },
    handleClick(event){
        let id = event.currentTarget.dataset.id;
        let musics = this.data.musics;
        let index = musics.findIndex(item =>{
            return item.id == id;
        })
        musics.splice(index,1);
        this.setData({
            musics:musics
        })
    }
})